<?php
namespace app\shop\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\service\lifeapiService;//生活号接口
use app\service\shopService;
use printing\printShop;
class Order extends Common{
	
	public function pay(){
		session("url",request()->url());
		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();
		//未绑定房屋
		if($userinfo["out_room_id"]=="0"){
			header("Location:https://www.jfzhiwen.com/shop/room/bind");
			return ;
		}
		
		$user['user_id'] = session('user_id');//user_id
		$shop_id=request()->get("shop_id");
		if(empty($shop_id)){
			return "商铺不存在";
		}
		$shopinfo=Db::table("zz_shop")->where("shop_id",$shop_id)->find();
		if(empty($shopinfo)){
			return "商铺不存在";
		}
		//var_dump($shopinfo);die;
		$this->assign("shopinfo",$shopinfo);
		$this->assign("user_id",$user['user_id']);
		//var_dump($result);die;
		return $this->fetch();
	}
			
	//创建订单唤起收银台
	public function createOrder(){
		
		$shop_id=request()->post("shop_id");//商铺id//
		$order_money=request()->post("ordermoney");//订单金额
		//查询商铺信息
		$shopinfo=Db::table("zz_shop")->where("shop_id",$shop_id)->find();
				
		$token="201907BBd3f00852d32b468db3c4667e7fdf0C73";
		$api = new lifeapiService();
		$order["out_trade_no"]=date('YmdHis').rand(10000,999999);
		$order["order_money"]=$order_money;
		$order["buyer_id"]=request()->post("user_id");
		//查询会员信息
		$userinfo=(new shopService())->userFind(session('user_id'));
		$notifyurl='https://www.jfzhiwen.com/shop/ali/notice';
		$result = $api->orderCreate(json_encode(['out_trade_no' =>$order['out_trade_no'], 'total_amount' =>  $order['order_money'], 'buyer_id' =>  $order['buyer_id'],'subject' => $shopinfo["shop_name"].'消费', 'token' => $token,'notifyurl' => $notifyurl]));
		if(property_exists($result,'sub_msg')){
			
			return json(['info' => $result->sub_msg]);
		}else{
			//添加进数据库
			$order['order_no'] =  $result->trade_no;
			$order['shop_id']=$shop_id;
			$order['shop_name']=$shopinfo['shop_name'];
			$order['create_time'] = date('Y-m-d H:i:s',time());
			$order['order_status'] = 0;
			$order['out_room_id']=$userinfo["out_room_id"];
			$order['room_address']=$userinfo["room_address"];
			$order['logon_id']=$userinfo["logon_id"];
			$order['user_name']=$userinfo["user_name"];
			$order['community_name']=$userinfo["community_name"];
			$order['community_id']=$userinfo["community_id"];
			$orderAdd = Db::table("zz_order_offline")->insert($order);
			//返回结果到前台唤起收银台
			if($orderAdd){
				return json(['order_no' => $order['order_no']]);
			}else{
				return json(['info' => '订单存储失败']);
			}
		}
	}

	public function shoporderlist(){
		session("url",request()->url());
		$shopinfo=Db::table('zz_shop')->where('user_id',session('user_id'))->find();
		
		if(empty($shopinfo)){
			return "您无权访问";
		}
		$where["shop_id"]=$shopinfo["shop_id"];
		$orderlist=(new shopService())->shoporderlist($where);
		$this->assign("orderlist",$orderlist);
		return $this->fetch('shop_order_list');
	}

	public function userorderlist(){
		session("url",request()->url());
		$userinfo=Db::table('zz_user')->where('user_id',session('user_id'))->find();
		//未绑定房屋
		if($userinfo["out_room_id"]=="0"){
			header("Location:https://www.jfzhiwen.com/shop/room/bind");
			return ;
		}

		$where["out_room_id"]=$userinfo["out_room_id"];
		$orderlist=(new shopService())->shoporderlist($where);
		$this->assign("orderlist",$orderlist);
		return $this->fetch('user_order_list');
	}
}
